package cn.new7as.cas5s.ui.easy;


import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import androidx.annotation.NonNull;


import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.http.EasyLog;
import com.hjq.http.config.IRequestHandler;
import com.hjq.http.exception.DataException;
import com.hjq.http.exception.HttpException;
import com.hjq.http.exception.NullBodyException;
import com.hjq.http.exception.ResponseException;
import com.hjq.http.request.HttpRequest;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.Map;


import cn.new7as.cas5s.R;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import okhttp3.Headers;
import okhttp3.Response;
import okhttp3.ResponseBody;

final public class RequestHandler implements IRequestHandler {
    private final Application mApplication;

    public RequestHandler(Application application) {
        mApplication = application;
    }

    @NonNull
    @Override
    public Object requestSuccess(@NonNull HttpRequest<?> httpRequest, @NonNull Response response,
                                 @NonNull Type type) throws Exception {
        if (Response.class.equals(type)) {
            return response;
        }

        if (!response.isSuccessful()) {
            throw new ResponseException(String.format(mApplication.getString(R.string.http_response_error),
                    response.code(), response.message()), response);
        }

        if (Headers.class.equals(type)) {
            return response.headers();
        }

        ResponseBody body = response.body();
        if (body == null) {
            throw new NullBodyException(mApplication.getString(R.string.response_null));
        }

        if (ResponseBody.class.equals(type)) {
            return body;
        }



        if (type instanceof GenericArrayType) {
            Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
            if (byte.class.equals(genericComponentType)) {
                return body.bytes();
            }
        }

        if (InputStream.class.equals(type)) {
            return body.byteStream();
        }

        if (Bitmap.class.equals(type)) {
            return BitmapFactory.decodeStream(body.byteStream());
        }

        String text;
        try {
            text = body.string();
        } catch (IOException e) {

            throw new DataException(mApplication.getString(R.string.http_data_wrong), e);
        }


        EasyLog.printJson(httpRequest, text);

        if (String.class.equals(type)) {
            return text;
        }

        final Object result;

        try {
            result = new Gson().fromJson(text, type);
        } catch (JsonSyntaxException e) {

            throw new DataException(mApplication.getString(R.string.http_data_wrong), e);
        }

        if (result instanceof HttpData) {
            HttpData model = (HttpData) result;

            if (model.getCode() == 0) {

                return result;
            } else if (model.getCode() == 700) {
                throw new TokenException(model.getMsg(), null);
            } else {

            }
            throw new HttpException(model.getMsg());
        } else {
            if (result instanceof Map) {
                String scac = ((Map<?, ?>) result).get("code").toString();

                    if (Double.valueOf(scac)==0) {

                    }else if (Double.valueOf(((Map<?, ?>) result).get("code").toString())==700) {
                        throw new TokenException(((Map<?, ?>) result).get("msg").toString(), null);
                    }else {
                        throw new HttpException(((Map<?, ?>) result).get("msg").toString());
                    }


            }

        }
        return result;
    }

    @NonNull
    @Override
    public Throwable requestFail(@NonNull HttpRequest<?> httpRequest, @NonNull Throwable e) {
        return e;
    }



    @Override
    public void clearCache() {

    }
}